package com.aos.eap.activity;

import android.os.Handler;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aos.eap.R;
import com.aos.eap.base.BaseDialogActivity;
import com.aos.eap.base.Constant;
import com.aos.eap.bean.CapBean;
import com.aos.eap.bean.RepairBean;
import com.aos.eap.bean.net.BaseResponseBean;
import com.aos.eap.utils.MyUtils;
import com.aos.eap.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 下劈刀
 */
public class EE_DownCleaverActivity extends BaseDialogActivity {


    @BindView(R.id.tvUser)
    TextView tvUser;
    @BindView(R.id.tvEqpType)
    TextView tvEqpType;
    @BindView(R.id.ivEqpTypeArrow)
    ImageView ivEqpTypeArrow;
    @BindView(R.id.flEqpType)
    FrameLayout flEqpType;
    @BindView(R.id.tvEqpId)
    TextView tvEqpId;
    @BindView(R.id.tvLotId)
    TextView tvLotId;
    @BindView(R.id.tvCleaverId)
    TextView tvCleaverId;
    @BindView(R.id.tvUseCount)
    TextView tvUseCount;
    @BindView(R.id.etRemark)
    FreeUI_ClearEditText etRemark;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private RepairBean repairBean;
    private CapBean capBean;
    private FreeUI_GeneralPop reasonPop;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_ee_down_cleaver;
    }

    @Override
    protected void initView() {
        tvUser.setText(Constant.CURRENT_USER);
        repairBean = (RepairBean) getIntent().getSerializableExtra("repairBean");
        tvEqpId.setText(repairBean.getEqpId());
        tvLotId.setText(repairBean.getLotId());
        tvCleaverId.setText(repairBean.getCapId());
        tvUseCount.setText(repairBean.getCapCnt());
        requestInfo();
    }

    private void requestInfo() {
        JsonObject map = new JsonObject();
        map.addProperty("EqpID", repairBean.getEqpId());
        NetUtils.requestGetNet(this, "EMS_RemoveCapLoad", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<CapBean>>(
                new TypeToken<BaseResponseBean<CapBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<CapBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    capBean = response.body().getResponse().getBody();
                    setPageData(capBean);
                }else {
                    capBean= null;
                    setPageData(new CapBean());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<CapBean>> response) {
                super.onError(response);
                capBean= null;
                setPageData(new CapBean());
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_RemoveCapLoad");
            }
        });
    }
    private void setPageData(CapBean capBean) {
        List<String> reasonList = new ArrayList<>();
        for (CapBean.ReasonBean entity : capBean.getReasonList()) {
            reasonList.add(entity.getReason());
        }
        reasonPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flEqpType.getWidth(),
                reasonList, ivEqpTypeArrow, (s, i) -> tvEqpType.setText(s));
    }
    @Override
    protected void initData() {

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("劈刀下机");
        freeUI_titleView.setLeftListener(view -> showBackDialog(EE_DownCleaverActivity.this));
        freeUI_titleView.setRightListener("提交", v -> {
            if (MyUtils.isFastClick()){
                showToast("您点击的太快了",false);
                return;
            }
            if (capBean == null) {
                showToast("未获取到劈刀信息，请重新扫描劈刀编号", false);
                return;
            }
            String reason = tvEqpType.getText().toString().trim();
            if (FreeApi_StringUtils.isEmpty(reason)) {
                showToast("请选择原因", false);
                return;
            }
            JsonObject map = new JsonObject();
            map.addProperty("EqpID", repairBean.getEqpId());
            map.addProperty("CapID", repairBean.getCapId());
            map.addProperty("LotID", repairBean.getLotId());
            map.addProperty("Reason", reason);
            map.addProperty("CapCnt", Float.parseFloat(repairBean.getCapCnt()));
            map.addProperty("Remark", etRemark.getText().toString().trim());
            requestSubmit(map);
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }

    private void requestSubmit(JsonObject map) {
        NetUtils.requestGetNet(this, "EMS_RemoveCap", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<?>>(
                new TypeToken<BaseResponseBean<?>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<?>> response) {
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    showToast("提交成功", true);
                    new Handler().postDelayed(() -> {
                        setResult(2104);
                        EE_DownCleaverActivity.this.finish();
                    }, 2000);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<?>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_RemoveCap");
            }
        });
    }

    @OnClick(R.id.flEqpType)
    public void onViewClicked() {
        if (reasonPop != null) {
            if (reasonPop.isShowing()) {
                reasonPop.dismiss();
            } else {
                FreeApi_PopUtils.setArrowShow(ivEqpTypeArrow);
                reasonPop.showAsDropDown(flEqpType, 0, 0);
            }
        }
    }
}
